package com.example.waterme.ui

import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.calculateStartPadding
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawing
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.waterme.R
import com.example.waterme.data.DataSource
import com.example.waterme.data.Plant
import com.example.waterme.ui.theme.WaterMeTheme

@Composable
fun HomeScreen(
    modifier: Modifier = Modifier,
    viewModel: PlantViewModel = hiltViewModel()
) {
    val plants = DataSource.plants
    val layoutDirection = LocalLayoutDirection.current
    Surface(
        modifier = modifier
            .fillMaxSize()
            .statusBarsPadding()
            .padding(
                start = WindowInsets.safeDrawing
                    .asPaddingValues()
                    .calculateStartPadding(layoutDirection),
                end = WindowInsets.safeDrawing
                    .asPaddingValues()
                    .calculateStartPadding(layoutDirection)
            )
    ) {
        var pointerOffset by remember { mutableStateOf(Offset(0f, 0f)) }
        PlantList(
            plants = plants,
            scheduleClick = viewModel::applySchedule,
            // TODO 这里创建了一个手电筒效果
            modifier = Modifier
                .pointerInput("dragging") {
                    detectDragGestures { _, dragAmount ->
                        pointerOffset += dragAmount
                    }
                }
                .onSizeChanged {
                    pointerOffset = Offset(it.width / 2f, it.height / 2f)
                }
                .drawWithContent {
                    drawContent()
                    drawRect(
                        Brush.radialGradient(
                            listOf(Color.Transparent, Color.Black),
                            center = pointerOffset,
                            radius = 100.dp.toPx()
                        )
                    )
                }
        )
    }
}

@Composable
private fun PlantList(
    plants: List<Plant>,
    scheduleClick: (seconds: Int, plantName: String) -> Unit,
    modifier: Modifier = Modifier
) {
    var scheduleDialog by remember { mutableStateOf(false) }
    var currentPlant: Plant? by remember { mutableStateOf(null) }
    LazyColumn(
        modifier = modifier,
        verticalArrangement = Arrangement.spacedBy(dimensionResource(id = R.dimen.padding_medium))
    ) {
        items(plants) { plant ->
            PlantCard(
                plant = plant,
                clickCard = {
                    currentPlant = plant
                    scheduleDialog = true
                },
                modifier = Modifier.fillMaxWidth()
            )
        }
    }

    if (scheduleDialog) {
        val plantName = stringResource(currentPlant!!.name)
        PlantDialog(
            plant = currentPlant!!,
            scheduleClick = {
                scheduleDialog = false
                scheduleClick(it, plantName)
            },
            onDismissRequest = {
                scheduleDialog = false
            }
        )
    }
}

@Composable
private fun PlantCard(
    plant: Plant,
    clickCard: () -> Unit,
    modifier: Modifier = Modifier
) {
    Card(
        modifier = modifier,
        onClick = clickCard
    ) {
        Column(
            modifier = Modifier.padding(dimensionResource(id = R.dimen.padding_medium)),
            verticalArrangement = Arrangement.spacedBy(dimensionResource(id = R.dimen.padding_small))
        ) {
            Text(
                text = stringResource(id = plant.name),
                textAlign = TextAlign.Center,
                style = MaterialTheme.typography.titleLarge,
                modifier = Modifier.fillMaxWidth()
            )
            Text(text = stringResource(id = plant.type))
            Text(text = stringResource(id = plant.description))
            Text(text = stringResource(id = plant.schedule))
        }
    }
}

@Composable
private fun PlantDialog(
    plant: Plant,
    scheduleClick: (seconds: Int) -> Unit,
    onDismissRequest: () -> Unit,
    modifier: Modifier = Modifier
) {
    Dialog(onDismissRequest = onDismissRequest) {
        Card(
            modifier = modifier.fillMaxWidth()
        ) {
            Column(
                modifier = Modifier.padding(dimensionResource(id = R.dimen.padding_medium)),
                verticalArrangement = Arrangement.spacedBy(dimensionResource(id = R.dimen.padding_small))
            ) {
                Text(
                    text = stringResource(id = R.string.remind_me, stringResource(id = plant.name)),
                    style = MaterialTheme.typography.titleLarge
                )

                Text(
                    text = stringResource(id = R.string.five_seconds),
                    modifier = Modifier
                        .fillMaxWidth()
                        .clickable { scheduleClick(5) }
                )
                Text(
                    text = stringResource(id = R.string.one_day),
                    modifier = Modifier
                        .fillMaxWidth()
                        .clickable { scheduleClick(86400) }
                )
                Text(
                    text = stringResource(id = R.string.one_week),
                    modifier = Modifier
                        .fillMaxWidth()
                        .clickable { scheduleClick(86400 * 7) }
                )
                Text(
                    text = stringResource(id = R.string.one_month),
                    modifier = Modifier
                        .fillMaxWidth()
                        .clickable { scheduleClick(86400 * 30) }
                )

                TextButton(
                    onClick = onDismissRequest,
                    modifier = Modifier.align(Alignment.End)
                ) {
                    Text(text = stringResource(id = R.string.dismiss))
                }
            }
        }
    }
}

@Preview(showBackground = true)
@Composable
fun HomeScreenPreview() {
    WaterMeTheme {
        HomeScreen()
    }
}
